<?php

// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------
namespace app\order\admin;

use app\admin\admin\Base;
use think\Db;
use service\Format;
use app\goods\model\GoodsOutofstock;
use app\goods\model\GoodsSku;

/**
 * 缺货登记
 * @author 晓风<215628355@qq.com>
 * @date 2021-11-6 17:13:57
 */
class Outofstock extends Base {

     /***
     * 缺货登记列表
     * @return mixed
     * @throws \think\exception\DbException
     * @author zhougs
     */
    public function goods_outofstock()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        // 查询
        $param = input("param.");
        if ($param['name']) {
            $map[] = ["g.name", 'like', '%' . $param['name'] . '%'];
        }
        if ($param['status'] != 'all' && $param['status'] !== '' && $param['status'] !== null) {
            $map[] = ["go.status", '=', $param['status']];
        }
        $map[] = ["go.is_del", '=', 0];
        // 数据列表
        $data_list = GoodsOutofstock::alias('go')
            ->join("goods g", "g.id = go.goods_id", "left")
            ->field("go.id,g.name,go.contacts,go.num,go.create_time,go.status,go.sku_id")
            ->where($map)
            ->order('go.id desc')
            ->paginate()
            ->each(
                function ($item) {
                    $item['key_name'] = GoodsSku::where("sku_id", $item['sku_id'])->value("key_name");
                    return $item;
                }
            );
        $fields = [
            ['id', 'ID'],
            ['contacts', lang('联系人')],
            ['name', lang('缺货商品名')],
            ['key_name', lang('规格')],
            ['num', lang('数量')],
            ['create_time', lang('登记时间')],
            ['status', lang('处理'), 'status', '', [lang('未处理'), lang('已处理')]],
            ['right_button', lang('操作'), 'btn']
        ];
        $searchFields = [
            ['name', lang('商品名称'), 'text'],
            ['status', lang('状态'), 'select', '', ['all' => lang('全部'), 0 => lang('未处理'), 1 => lang('已处理')]],
        ];
        return Format::ins()//实例化
           ->hideCheckbox()
            ->addColumns($fields)//设置字段
            ->setTopSearch($searchFields)
            ->setRightButtons([
                ['ident' => 'outofstock_detail', 'title' => lang('查看'), 'href' => ['outofstock_detail', ['id' => '__id__']], 'icon' => 'fa fa-pencil pr5', 'class' => 'btn btn-xs mr5 btn-default layeredit'],
                ['ident' => 'outofstock_delete', 'title' => lang('删除'), 'href' => ['outofstock_delete', ['ids' => '__id__']], 'icon' => 'fa fa-times pr5', 'class' => 'btn btn-xs mr5 btn-default   ajax-get confirm'],
            ])
            ->setData($data_list)//设置数据
            ->fetch();//显示
    }
    /**
     * 缺货登记
     * @author  zhougs
     */
    public function goods_outofstock_add()
    {
        $is_post = $this->request->isPost();
        if (!$is_post) {
            $this->error(lang('参数错误'));
        }
        $param = $this->request->post();
        $contacts = Db::name("admin")->where("id", UID)->value("username");
        $data = [
            "goods_id" => $param['goods_id'],
            "sku_id" => $param['sku_id'],
            "num" => $param['num'],
            "contacts" => $contacts,
            "create_time" => time(),
        ];
        $res = GoodsOutofstock::create($data);
        if ($res) {
            $this->success(lang('登记成功'), "/admin.php/order/order/express_index");
        }
        $this->error(lang('登记失败'));
    }

    public function outofstock_detail($id)
    {
        if ($id === null) {
            $this->error(lang('缺少参数'));
        }

        // 保存数据
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();

            $data['admin_id'] = UID;
            $data['status'] = 1;
            if (GoodsOutofstock::update($data)) {
                $this->success(lang('处理成功'), cookie('__forward__'));
            } else {
                $this->error(lang('处理失败'));
            }
        }

        $info = GoodsOutofstock::alias('go')
            ->join("goods g", "g.id = go.goods_id", "left")
            ->join("goods_sku gs", "go.sku_id = gs.sku_id", "left")
            ->field("go.id,g.name,go.contacts,go.num,go.create_time,go.status,go.describe,go.remark,gs.key_name")
            ->where("go.id", $id)
            ->find();
        $fields = [
            ['type' => 'hidden', 'name' => 'id'],
            ['type' => 'text', 'name' => 'name', 'title' => lang('缺货商品名'), 'tips' => '', 'attr' => 'disabled', 'value' => ''],
            ['type' => 'text', 'name' => 'key_name', 'title' => lang('规格'), 'tips' => '', 'attr' => 'disabled', 'value' => ''],
            ['type' => 'text', 'name' => 'create_time', 'title' => lang('登记时间'), 'tips' => '', 'attr' => 'disabled', 'value' => ''],
            ['type' => 'text', 'name' => 'num', 'title' => lang('数量'), 'tips' => '', 'attr' => 'disabled', 'value' => ''],
            ['type' => 'text', 'name' => 'contacts', 'title' => lang('联系人'), 'tips' => '', 'attr' => 'disabled', 'value' => ''],
            ['type' => 'text', 'name' => 'describe', 'title' => lang('详细描述'), 'tips' => '', 'attr' => 'disabled', 'value' => ''],
            ['type' => 'text', 'name' => 'remark', 'title' => lang('处理备注'), 'tips' => '', 'attr' => '', 'value' => ''],

        ];
        $this->assign('page_title', lang('查看详情'));
        $this->assign('form_items', $this->setData($fields, $info));
        return $this->fetch('admin@public/edit');
    }
    
     public function outofstock_delete($ids)
    {
        $newIds = $ids;
        if (is_numeric($ids)) {
            $newIds = [];
            $newIds[] = $ids;
        }
        Db::startTrans();
        try {
            foreach ($newIds as $k => $v) {
                GoodsOutofstock::where(['id' => $v])->setField('is_del', 1);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error(lang('删除失败'));
        }
        $this->success(lang('删除成功'));
    }

}
